<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>限流函数</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://unpkg.com/lodash@4.17.20/lodash.min.js"></script>
</head>
<body>
    <div id="Application">
        <button @click="click">按钮</button>
        <button @click="throttleClick">限流按钮</button>
        <button @click="debounceClick">防抖按钮</button>
    </div>
    <script>
        const {createApp, ref} = Vue
        var throttle = false
        function throttleTool(callback, timeout) {
            if (!throttle) {
                callback()
            } else {
                return
            }
            throttle = true
            setTimeout(() => {
                throttle = false
            }, timeout)
        }
        const config = {
            setup() {
                // 控制按钮是否可触发的变量
                const throttle = ref(false)
                const click = () => {
                    throttleTool(()=>{
                        console.log(Date())
                    }, 2000)
                }
                const throttleClick = _.throttle(function(){
                    console.log(Date())
                }, 2000)
                const debounceClick = _.debounce(function(){
                    console.log(Date())
                }, 2000)
                return {throttle, click, throttleClick, debounceClick}
            }
        }
        createApp(config).mount("#Application") 
    </script>
</body>
</html>